package com.example.elderlycare.controller;

import com.example.elderlycare.controller.entity.RewardRecordRequestBody;
import com.example.elderlycare.dao.entity.RewardRecord;
import com.example.elderlycare.service.RewardService;
import com.example.elderlycare.service.entity.RewardRecordListResponseBody;
import com.example.elderlycare.utils.Result;
import com.example.elderlycare.utils.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/rewards")
public class RewardsController {
    @Resource
    RewardService rewardService;

    /**
     * 查询奖惩记录
     *
     * @param employeeName 员工名字
     * @param rewardsType  奖惩类型
     * @param startDate    根据日期范围查询开始日期
     * @param endDate      根据日期范围查询结束日期
     * @param pageSize     页大小
     * @param current      当前页
     * @return RewardRecordListResponseBody
     */
    @GetMapping
    public Result<RewardRecordListResponseBody> queryRewards(
            @RequestParam(value = "employeeName", required = false) String employeeName,
            @RequestParam(value = "rewardsType", required = false) String rewardsType,
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
            @RequestParam(defaultValue = "1", value = "current") int current
    ) {
        Map<String, Object> params = new HashMap<>();
        params.put("employeeName", employeeName != null ? "%" + employeeName + "%" : null);
        params.put("rewardsType", rewardsType);
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("limit", pageSize);
        params.put("offset", (current - 1) * pageSize);
        try {
            RewardRecordListResponseBody res = rewardService.queryRewardRecords(params);
            return Result.success(res);
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 新增奖惩记录
     *
     * @param req RewardRecordRequestBody.addRewardRecord
     * @return null
     */
    @PostMapping
    public Result<?> addRewards(@RequestBody RewardRecordRequestBody.addRewardRecord req) {
        RewardRecord rewardRecord = new RewardRecord();
        BeanUtils.copyProperties(req, rewardRecord);
        rewardRecord.setRewardsDate(new Date());
        try {
            boolean flag = rewardService.save(rewardRecord);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("add RewardRecord error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 修改奖惩记录
     *
     * @param req RewardRecordRequestBody.updateRewardRecord
     * @return null
     */
    @PutMapping
    public Result<?> updateRewards(@RequestBody RewardRecordRequestBody.updateRewardRecord req) {
        RewardRecord rewardRecord = new RewardRecord();
        BeanUtils.copyProperties(req, rewardRecord);
        try {
            boolean flag = rewardService.updateById(rewardRecord);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("update RewardRecord error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 删除奖惩记录
     *
     * @param id 记录id
     * @return null
     */
    @DeleteMapping
    public Result<?> deleteRewards(@RequestParam("id") @NotEmpty Integer id) {
        try {
            boolean flag = rewardService.removeById(id);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("delete RewardRecord error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }
}
